<?php
class lib_company_user extends lib_base{
	public $table_name = "qc_company_user";
    public $pk = "id";


    public function check_login($tel)
    {
        // 查询用户是否已存在
        $user = $this->find(array("tel='{$tel}'"));
        if(!$user){
            $company_id = 0;
            if($_SESSION['company_id']){
                $company_id = $_SESSION['company_id'];
            }
            $arrBase = array(
                'company_id'=>$company_id,
                'tel'=>$tel,
                'status'=>1,
                'add_time' => date('Y-m-d H:i:s'),
            );
            $user_id = $this->add($arrBase);
        }else{
            $user_id = $user['id'];
            $arrBase = array(
                'up_time'=>date('Y-m-d H:i:s'),
            );
            $this->edit($user_id,$arrBase);
        }
        $this->token = pp_encrypt('user_'.$user_id);
        // 记录推荐人关系
        // if (!$user && $refereeId > 0) {
        //     RefereeModel::createRelation($model['user_id'], $refereeId);
        // }

        $data['user_id'] = $user_id;
        // $data['user_tel'] = $user_tel;
        $data['user_token'] = $this->token;
        return $data;
    }


    public function add($data)
    {
        $data['status'] = 1;
        $data['add_time'] = date('Y-m-d H:i:s');
        return $this->create($data);
    }

    public function get_pname($id)
    {
        $info = $this->findById($id);
        return $this->create($data);
    }

}